package com.base.controller;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.text.StrPool;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.base.common.AjaxResult;
import com.base.common.BaseController;
import com.base.common.TableDataInfo;
import com.base.domain.SysDict;
import com.base.service.SysDictService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;

/**
 * @author gxd
 * @since 2025/6/6
 */
@Slf4j
@RestController
@RequestMapping("/dict")
@Api(tags = "字典信息")
public class SysDictController extends BaseController {

    @Autowired
    private SysDictService dictService;

    @ApiOperation("分页列表-字典列表")
    @PostMapping("/pageList")
    public TableDataInfo pageList(@ApiParam(value = "字典查询条件") SysDict dict) {
        startPage(dict.getPageNum(), dict.getPageSize());
        List<SysDict> dictList = dictService.pageList(dict);
        return getDataTable(dictList);
    }

    @ApiOperation("详情信息-根据Id获取字典详情信息")
    @ApiImplicitParam(name = "id", value = "字典Id", required = true, dataType = "Long", paramType = "path")
    @GetMapping("/getById/{id}")
    public AjaxResult getById(@PathVariable Long id) {
        return success(dictService.getById(id));
    }

    @ApiOperation("添加或修改-字典信息")
    @PostMapping("/addOrEdit")
    public AjaxResult addOrEdit(@ApiParam(value = "字典实体", required = true) @Valid @RequestBody SysDict dict) {
        return success(dictService.addOrEdit(dict));
    }

    @ApiOperation("删除-字典信息（支持批量）")
    @ApiImplicitParam(name = "ids", value = "字典Id（多个需逗号分隔）", required = true, allowMultiple = true, dataType = "String", paramType = "path")
    @DeleteMapping("/removeById")
    public AjaxResult removeById(@RequestParam("ids") String ids) {
        Assert.notEmpty(ids, "缺少必要参数：请提供要删除的记录ID");
        return toAjax(dictService.remove(Wrappers.lambdaQuery(SysDict.class).in(SysDict::getId, ids.split(StrPool.COMMA))));
    }
}
